package com.qtt.app.controller;

import com.qtt.app.common.ApiResponse;
import com.qtt.app.entity.AppInstitution;
import com.qtt.app.entity.RequestData.AddInstitution;
import com.qtt.app.service.AppInstitutionService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "机构管理", description = "InstitutionController")   //tags是api名字，description是简介，value:已废用
@RestController
@RequestMapping("/Institution")
public class AppInstitutionController {
    @Autowired
    AppInstitutionService appInstitutionService;

    @ApiOperation(value = "查询所有机构(首页)", notes = "可以查institution表中的所有数据")  //value是Get /Msg/list旁边的那个api简介；notes是点开的介绍，可用于详细说明api作用
    @GetMapping("/list")
    public ApiResponse<List<AppInstitution>> listall(){
        appInstitutionService.listAll();
        List<AppInstitution> entities = appInstitutionService.listAll();
        return ApiResponse.<List<AppInstitution>>builder().code(200).message("查看成功").data(entities).build();
    }

    @ApiOperation(value = "添加一条新的记录", notes = "institution_name必填，且不重复")
    @PostMapping("/list")
    public ApiResponse<String> addMsg(@ApiParam(value="机构信息：institutionName机构名字;introduction机构介绍;logoPicture机构logo图片地址;institutionGrade机构评分;courseSum机构课程总数;teacherSum机构讲师总数;soldCourseSum机构售出课程总数",required = true) @RequestBody(required=true) AddInstitution requestData){
        //判断，如果传入的是默认值，需要设置为null或者默认图片
        AppInstitution entity = new AppInstitution();
        entity.setInstitutionName(requestData.getInstitutionName());
        entity.setIntroduction(requestData.getIntroduction());
        entity.setLogoPicture(requestData.getLogoPicture());
        entity.setInstitutionGrade(requestData.getInstitutionGrade());
        entity.setCourseSum(requestData.getCourseSum());
        entity.setTeacherSum(requestData.getTeacherSum());
        entity.setSoldCourseSum(requestData.getSoldCourseSum());

        int b = (appInstitutionService.addInstitution2(entity));
        if (b == 1){
            return ApiResponse.<String>builder().code(200).message("添加成功").data("success").build();
        }
        else {
            return ApiResponse.<String>builder().code(400).message("添加失败").data("fail").build();
        }
    }
//    @ApiOperation(value = "添加一条新的记录", notes = "institution_name必填，且不重复")
//    @PostMapping("/list")
//    public ApiResponse<String> addMsg(@ApiParam(value="机构名字，必填",required = true) @RequestParam(value="institutionName",required=true) String institutionName,
//                                      @ApiParam(value="机构介绍",required = true) @RequestParam(value="introduction",required=true) String introduction,
//                                      @ApiParam(value="机构logo图片地址",required = false) @RequestParam(value="logoPicture",required=false,defaultValue ="https://qtt-cmall.oss-accelerate.aliyuncs.com/course/202103/1616047449252v2-1dfcfa807600f4e5e7983941bd115f2c_r.jpg") String logoPicture,
//                                      @ApiParam(value="机构评分",required = true) @RequestParam(value="institutionGrade",required=true) Float institutionGrade,
//                                      @ApiParam(value="机构课程总数",required = false) @RequestParam(value="courseSum",required=false,defaultValue = "10") Integer courseSum,
//                                      @ApiParam(value="机构讲师总数",required = false) @RequestParam(value="teacherSum",required=false,defaultValue = "10") Integer teacherSum,
//                                      @ApiParam(value="机构售出课程总数",required = true) @RequestParam(value="soldCourseSum",required=true) Integer soldCourseSum){
//        //判断，如果传入的是默认值，需要设置为null或者默认图片
//        AppInstitution entity = new AppInstitution();
//        entity.setInstitutionName(institutionName);
//        entity.setIntroduction(introduction);
//        entity.setLogoPicture(logoPicture);
//        entity.setInstitutionGrade(institutionGrade);
//        entity.setCourseSum(courseSum);
//        entity.setTeacherSum(teacherSum);
//        entity.setSoldCourseSum(soldCourseSum);
//
//        int b = (appInstitutionService.addInstitution2(entity));
//        if (b == 1){
//            return ApiResponse.<String>builder().code(200).message("添加成功").data("success").build();
//        }
//        else {
//            return ApiResponse.<String>builder().code(400).message("添加失败").data("fail").build();
//        }
//    }

    @ApiOperation(value = "根据id查找一条记录", notes = "")
    @GetMapping("/list/{id}")
    public ApiResponse<AppInstitution> getInstitutionById(@PathVariable("id") Integer institutionId){
        AppInstitution entity = appInstitutionService.getById(institutionId);

        if (entity != null){
            return ApiResponse.<AppInstitution>builder().code(200).message("查找成功").data(entity).build();
        }
        else {
            return ApiResponse.<AppInstitution>builder().code(400).message("查找失败").data(null).build();
        }
    }

    @ApiOperation(value = "修改一条记录", notes = "只传需要修改的参数,不修改的参数不用传、会自动使用默认值的")
    @PutMapping("/list/{id}")
    public ApiResponse<String> updateUserById(@ApiParam(value="机构id",required = true) @PathVariable("id") Integer id,
                                              @ApiParam(value="修改机构信息：不修改的就删掉，只传要修改的字段",required = true) @RequestBody(required=true) AddInstitution requestData){
        //先以userId往用户表查找，如果不存在，则无法修改；如果存在，在原来的记录上更新需要修改的字段即可。
        Integer institutionId=id;
        AppInstitution entity = appInstitutionService.getById(institutionId);
        if (entity ==null){
            return ApiResponse.<String>builder().code(400).message("修改失败,输入的id有误，该机构不存在").data("fail").build();
        }
        System.out.println(entity);

        if(requestData.getInstitutionName()!=null ){
            entity.setInstitutionName(requestData.getInstitutionName());
        }
        if(requestData.getIntroduction() !=null){
            entity.setIntroduction(requestData.getIntroduction());
        }
        if(requestData.getLogoPicture()!=null){
            entity.setLogoPicture(requestData.getLogoPicture());
        }
        if(requestData.getInstitutionGrade()!=null){
            entity.setInstitutionGrade(requestData.getInstitutionGrade());
        }
        if(requestData.getCourseSum() != null){
            entity.setCourseSum(requestData.getCourseSum());
        }
        if(requestData.getTeacherSum()!=null){
            entity.setTeacherSum(requestData.getTeacherSum());
        }
        if(requestData.getSoldCourseSum()!=null){
            entity.setSoldCourseSum(requestData.getSoldCourseSum());
        }
        System.out.println("第二次：");
        System.out.println(entity);
        Boolean b = appInstitutionService.updateById(id,entity);
        if (b == true){
            return ApiResponse.<String>builder().code(200).message("修改成功").data(b.toString()).build();
        }
        else {
            return ApiResponse.<String>builder().code(400).message("修改失败").data(b.toString()).build();
        }
    }

    @ApiOperation(value = "根据课程id查找它所属的机构信息(课程详情页)", notes = "注意此课程id传入的是generalId")
    @GetMapping("/getInstByCourse")
    public ApiResponse<AppInstitution> getInstByCourse(@ApiParam(value="课程generalId",required = true) @RequestParam(value="generalId",required=true) String generalId){

        AppInstitution entity = appInstitutionService.getInstByCourse(generalId);

        if (entity != null){
            return ApiResponse.<AppInstitution>builder().code(200).message("查找成功").data(entity).build();
        }
        else {
            return ApiResponse.<AppInstitution>builder().code(400).message("查找失败,请检查传入的参数").data(null).build();
        }
    }

//    @ApiOperation(value = "修改一条记录", notes = "只传需要修改的参数,不修改的参数不用传、会自动使用默认值的")
//    @PutMapping("/list/{id}")
//    public ApiResponse<String> updateUserById(@ApiParam(value="机构id") @PathVariable(value="id") Integer id,
//                                               @ApiParam(value="机构名字",required = false) @RequestParam(value="institutionName",required=false,defaultValue = "不修改则不填") String institutionName,
//                                               @ApiParam(value="机构介绍",required = false) @RequestParam(value="introduction",required=false,defaultValue = "不修改则不填") String introduction,
//                                               @ApiParam(value="机构logo图片地址（不修改则不填）",required = false) @RequestParam(value="logoPicture",required=false,defaultValue ="不修改则不填") String logoPicture,
//                                               @ApiParam(value="机构评分",required = false) @RequestParam(value="institutionGrade",required=false,defaultValue = "-1.0") Float institutionGrade,
//                                               @ApiParam(value="机构课程总数",required = false) @RequestParam(value="courseSum",required=false,defaultValue = "-1") Integer courseSum,
//                                               @ApiParam(value="机构讲师总数",required = false) @RequestParam(value="teacherSum",required=false,defaultValue = "-1") Integer teacherSum,
//                                               @ApiParam(value="机构售出课程总数",required = false) @RequestParam(value="soldCourseSum",required=false,defaultValue = "-1") Integer soldCourseSum){
//        //先以userId往用户表查找，如果不存在，则无法修改；如果存在，在原来的记录上更新需要修改的字段即可。
//        Integer institutionId=id;
//        AppInstitution entity = appInstitutionService.getById(institutionId);
//        if (entity ==null){
//            return ApiResponse.<String>builder().code(400).message("修改失败,输入的id有误，该机构不存在").data("fail").build();
//        }
//        System.out.println(entity);
//
//        if(institutionName.equals("不修改则不填")==false ){
//            entity.setInstitutionName(institutionName);
//        }
//        if(introduction.equals("不修改则不填")==false){
//            entity.setIntroduction(introduction);
//        }
//        if(logoPicture.equals("不修改则不填")==false){
//            entity.setLogoPicture(logoPicture);
//        }
//        if(institutionGrade != -1.0){
//            entity.setInstitutionGrade(institutionGrade);
//        }
//        if(courseSum!=-1){
//            entity.setCourseSum(courseSum);
//        }
//        if(teacherSum != -1){
//            entity.setTeacherSum(teacherSum);
//        }
//        if(soldCourseSum != -1){
//            entity.setSoldCourseSum(soldCourseSum);
//        }
//        System.out.println("第二次：");
//        System.out.println(entity);
//        Boolean b = appInstitutionService.updateById(id,entity);
//        if (b == true){
//            return ApiResponse.<String>builder().code(200).message("修改成功").data(b.toString()).build();
//        }
//        else {
//            return ApiResponse.<String>builder().code(400).message("修改失败").data(b.toString()).build();
//        }
//    }

//    @ApiOperation(value = "课程标签页的推荐机构，按照评分推荐", notes = "")
//    @GetMapping("/RecommendCourse")
//    public ApiResponse<List<AppInstitution>> getRecommendCourse(){
//
//        List<AppInstitution> entities = appInstitutionService.getRecommendCourse();
//
//        if (entities.size() != 0){
//            return ApiResponse.<List<AppInstitution>>builder().code(200).message("查看成功").data(entities).build();
//        }
//        else {
//            return ApiResponse.<List<AppInstitution>>builder().code(400).message("查找失败,请检查传入的参数").data(null).build();
//        }
//    }

}
